var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contacts: [],
    winHeight: "",
    number:0,
    sure:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight,
          number:options.number
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    this.getData(that);
  },
  getData: function (that) {

    util.request(api.ContactsList, { token: wx.getStorageSync("token") }).then(function (res) {
      if (res.code == '200') {
        var contacts = res.datas;
        for(var i=0;i<contacts.length;i++){
          contacts['check']=false;
        }
        that.setData({
          contacts: contacts,
          sure:false
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  fnToNew: function () {
    wx.navigateTo({
      url: '/pages/perCenter/contactsNew/index',
    })
  },

  fnEdit: function (e) {
    var id = e.currentTarget.dataset.id
    var name = e.currentTarget.dataset.name
    var telephone = e.currentTarget.dataset.telephone
    var idCard = e.currentTarget.dataset.idcard
    var isdefault = e.currentTarget.dataset.isdefault
    wx.navigateTo({
      url: '/pages/perCenter/contactsNew/index?id=' + id + "&name=" + name + "&telephone=" + telephone + "&idCard=" + idCard +"&isdefault=" + isdefault
    })
  },
  fnDel: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var obj = new Object();
    obj.id = id;
    wx.showLoading({
      title: '请稍候',
    })
    util.request(api.ContactsOper, { token: wx.getStorageSync("token"), contacts: obj, type: "del" }).then(function (res) {
      wx.hideLoading()
      if (res.code == '200') {
        that.getData(that)
      }
    });
  },
  //选中、取消
  updateCheck: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var index = parseInt(e.currentTarget.dataset.index);
    var contacts = that.data.contacts;
    contacts[index]['check']=!contacts[index].check;
    var j=0;
    for(var i=0;i<contacts.length;i++){
      if(contacts[i].check){
        j++;
      }
    }
    var sure = false;
    if(j>that.data.number){
      contacts[index]['check']=false;
      sure=true;
    }else if(j==that.data.number){
      sure = true;
    }
    that.setData({
      contacts:contacts,
      sure:sure
    })
  },
  fnSureChoose:function(){
    var that = this;
    if(!that.data.sure){
      return;
    }
    var contacts = that.data.contacts;
    var prePage = getCurrentPages()[getCurrentPages().length-2];
    var chooseC = [];
    var contacts = that.data.contacts;
    for(var i=0;i<contacts.length;i++){
      if(contacts[i].check){
        contacts[i]['tel'] = contacts[i].telephone;
        chooseC.push(contacts[i])
      }
    }
    prePage.setData({
      userList:chooseC
    });
    wx.navigateBack({})
  }
})